const Token = appRequire('common/token');
const wxMini = appRequire('common/wx/wxMini');
const { Op } = require("sequelize");

const userModel = appRequire('models/user');

// 导出对象，供其它模块调用
module.exports = {
    list,
    confirm
};
//用户列表
async function list(req, res) {
    try {
        let postData = req.body

        // 参数验证
        if (!postData.pageSize || !postData.pageNum) {
            const errorData = Common.clone(Constant.LACK);
            errorData.msg = '缺少必要参数：pageSize 和 pageNum';
            return res.json(errorData);
        }

        // 设置默认值
        const pageSize = parseInt(postData.pageSize) || 10;
        const pageNum = parseInt(postData.pageNum) || 1;
        
        const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

        let params = {}
        if (postData.isCreater != "全部") {
            if (postData.isCreater == "已通过") params.isCreater = 1
            if (postData.isCreater == "申请中") params.isCreater = 0
            if (postData.isCreater == "未申请") params.isCreater = -1
        }
        if (postData.searchText) params.nickName = { [Op.like]: '%' + postData.searchText + '%' }
        
        let result2 = await userModel.user.findAndCountAll({
            where: params,
            limit: pageSize,
            offset: pageSize * (pageNum - 1),
            order: [['createdAt', 'DESC']],
        })
        result2 = Common.clone(result2)

        returnData.maxCount = result2.count
        returnData.maxPage = Math.ceil(result2.count / pageSize)
        returnData.data = result2.rows
        return res.json(returnData)
    } catch (error) {
        console.error('用户列表查询错误:', error);
        const errorData = Common.clone(Constant.DEFAULT_ERROR);
        errorData.msg = '查询用户列表失败: ' + error.message;
        return res.json(errorData);
    }
}

//通过创作者
async function confirm(req, res) {
    try {
        let postData = req.body
        const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

        let result1 = Common.checkUrl(postData, ['userId'], res);
        if (result1 == false) return;

        let result2 = await userModel.user.findOne({
            where: {
                objectId: postData.userId
            }
        })

        if (!result2) {
            const errorData = Common.clone(Constant.DEFAULT_ERROR);
            errorData.msg = '用户不存在';
            return res.json(errorData);
        }

        result2 = Common.clone(result2)

        if (result2.isCreater == 0) {
            let result3 = await userModel.user.update({
                isCreater: 1
            }, {
                where: {
                    objectId: postData.userId
                }
            })
            returnData.data = Common.clone(result3)
            return res.json(returnData)
        } else if (result2.isCreater == 1) {
            returnData.data = Common.clone(result2)
            returnData.code = 0
            returnData.msg = "已通过"
            return res.json(returnData)
        } else if (result2.isCreater == -1) {
            returnData.data = Common.clone(result2)
            returnData.code = 0
            returnData.msg = "未申请"
            return res.json(returnData)
        }
    } catch (error) {
        console.error('确认创作者错误:', error);
        const errorData = Common.clone(Constant.DEFAULT_ERROR);
        errorData.msg = '确认创作者失败: ' + error.message;
        return res.json(errorData);
    }
}
